//
//  MovieCell.m
//  FiveWork
//
//  Created by lanou3g on 15/11/24.
//  Copyright © 2015年 Project. All rights reserved.
//

#import "MovieCell.h"
@import AVFoundation;
#import "MovieManger.h"
#import "MovieModel.h"
#import <UIImageView+WebCache.h>
@interface MovieCell ()
@property (weak, nonatomic) IBOutlet UIButton *playButton;


@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *dayLabel;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *movieImageView;
@property(nonatomic,strong)UIImageView *mImageView;

@property(nonatomic,strong)MovieModel *model;
@property(nonatomic,strong)MovieManger *movieMange;

@end


@implementation MovieCell

-(void)setupViewsWithModel:(MovieModel*)movie
{
    self.model=[[MovieModel alloc] init];
    self.model =movie;
    self.titleLabel.text =movie.text;
    self.nameLabel.text =movie.name;
    self.dayLabel.text =movie.create_time;
    
    self.movieMange =[MovieManger defaultMovieManager];
    

    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:movie.profile_image]];
    
//    [self.movieImageView sd_setImageWithURL:[NSURL URLWithString:movie.bimageuri]];
    self.movieImageView.backgroundColor =[UIColor whiteColor];
  
    self.button =[UIButton buttonWithType:UIButtonTypeSystem];
//    [self.button addSubview:self.mImageView];
    
     self.mImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.movieImageView.frame.size.width -5, self.movieImageView.frame.size.height)];
   [self.mImageView sd_setImageWithURL:[NSURL URLWithString:movie.bimageuri]];
    [self.movieImageView addSubview:self.mImageView];
    [self.mImageView setUserInteractionEnabled:YES];

}

- (IBAction)clickPlayButtonAction:(UIButton *)sender {
    [self.movieMange playerPause];
    
    if ([sender.titleLabel.text isEqualToString:@""]) {
        [self.movieMange playerPause];
        [sender setBackgroundImage:[UIImage imageNamed:@"video_play_medium"] forState:UIControlStateNormal];
        self.playButton.titleLabel.text = @"playButton";
        

    }else{
        
        
        
        self.button =sender;
        [self.movieMange shiFang];
        [self.movieMange setupAVPlayerWithModel:self.model];
        [self setupPlayer];
        sender.titleLabel.text =@"";
        [self.playButton setBackgroundImage:nil forState:UIControlStateNormal];
        [self.movieMange playerPlay];
    }
    
    
}
/**
 设置播放器
 */
- (void)setupPlayer {
    
    self.movieMange.avPlayerLayer.frame =self.mImageView.frame;
    [self.mImageView.layer addSublayer:self.movieMange.avPlayerLayer];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
